package trace

import (
	"os"
	"log"
	"fmt"
	"sync"
	"github.com/huandu/goroutine"
)

var logfile * os.File
var lock     sync.Mutex

func init() {
	file, err  := os.Create("/tmp/trace.log")
	if err != nil {
		log.Fatal(err)
		return
	}

	logfile = file
}

func MethodEntry(methodName string, argNames []string, argValues []interface{}, printArg bool){

	lock.Lock()
	fmt.Fprintf(logfile, "MethodEntry name=%s pid=%d goroutine=%d\n", methodName, os.Getpid(), goroutine.GoroutineId())

	if printArg {
		for index, argName := range argNames {
			argValue := argValues[index]
			fmt.Fprintln(logfile, argName + "=", argValue)
		}
	}

	lock.Unlock()

}

func MethodExit(methodName string){
	lock.Lock()
	fmt.Fprintf(logfile, "MethodExit name=%s pid=%d goroutine=%d\n", methodName, os.Getpid(), goroutine.GoroutineId())
	lock.Unlock()
}

